<?php
include("constants.php");
include("Item.class.php");
include("ItemList.class.php");
include("Database.class.php");

class Cart {
	public $itemList;
	public $subTotalPrice;
	public $totalShipping;
	public $totalPrice;
	public $numOfItems;
	
    function Cart() {
		session_start();
		//session_destroy();
    	
    	$this->itemList = new ItemList();
    	
    	// Check if there are currently items in the cart
    	if(isset($_SESSION['easycart'])) {
    		$this->addAllItems();
    	}
    	
    	// Check for action directives from the GET variable
    	if(isset($_GET['displayCart'])) {
    		$this->displayCart();
    	}
    	if(isset($_GET['add'])) {
    		$this->addToCart($_GET['add']);
    	}
    	if(isset($_GET['remove'])) {
    		$this->removeItem($_GET['remove']);
    	}
    	if(isset($_GET['id']) && isset($_GET['quantity'])) {
    		$this->updateQuantity($_GET['id'], $_GET['quantity']);
    	}
    	if($this->isEmpty()) {
    		session_destroy();
    	}
    	
    	$this->updateTotals();
    	$this->save();
    	header("Location: ".CART_URL);
    }
    
    /**
     * addAllItems - gets all items out of session variable
     * and stores them in the ItemList object
     */
    function addAllItems() {
    	foreach($_SESSION['easycart']['items'] as $item) {
    			$this->itemList->add(new Item($item['id'], $item['quantity']));
    	}
    }
    
    /**
     * addToCart - adds a single item to te current shopping cart
     */    
    function addToCart($itemId) {
    	$this->itemList->add(new Item($itemId));
    }
    
    /**
     * displayCart - display contents of shopping cart
     * (for testing purposes)
     */
    function displayCart() {
    	include('templates/view_cart.php');
    	exit;
    }
    
    /**
     * getNumOfItems - returns the number of items in the cart
     */
    function getNumOfItems() {
    	return $this->itemList->getNumOfItems();
    }
    
    /**
     * isEmpty - returns true if the cart is empty. Returns
     * false otherwise.
     */
    function isEmpty() {
    	return $this->itemList->isEmpty();
    }
    
    /**
     * remove - remove a single item from the cart
     */
    function removeItem($id) {
    	$this->itemList->remove($id);
    }
    
    /**
     * save - stores contents of cart in session variable
     */
    function save() {
    	$_SESSION['easycart']['items'] = $this->itemList->toArray();
    	$_SESSION['easycart']['subTotalPrice'] = money_format(MONEY_FORMAT_STRING, $this->subTotalPrice);
    	$_SESSION['easycart']['totalShipping'] = money_format(MONEY_FORMAT_STRING, $this->totalShipping);
    	$_SESSION['easycart']['totalPrice'] = money_format(MONEY_FORMAT_STRING, $this->totalPrice);
    	$_SESSION['easycart']['numOfItems'] = $this->itemList->getNumOfItems();
    }
    
    /**
     * updateQuantity - updates the quantity of given item
     */
    function updateQuantity($id, $quantity) {
    	$this->itemList->getItem($id)->setQuantity($quantity);
    }
    
    /**
     * updateTotals - updates the total price and total shipping of cart
     */
    function updateTotals() {
    	$total = 0;
    	$shipping = 0;
    	foreach($this->itemList->getItemList() as $item) {
    		$total += $item->totalPrice;
    		$shipping += $item->totalShipping;
    	}
    	$this->subTotalPrice = $total;
    	$this->totalShipping = $shipping;
    	$this->totalPrice = $total + $shipping;
    }
}

$cart = new Cart();
?>